package com.xscha.tableforge.service.impl;

import com.xscha.tableforge.model.entity.Space;
import com.xscha.tableforge.model.vo.PageReq;
import com.xscha.tableforge.model.vo.PageResult;
import com.xscha.tableforge.repository.SpaceRepository;
import com.xscha.tableforge.service.BaseService;
import com.xscha.tableforge.service.SpaceService;
import com.xscha.tableforge.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 空间服务实现类
 */
@Service
public class SpaceServiceImpl extends BaseService<Space> implements SpaceService {

    private final SpaceRepository spaceRepository;

    @Autowired
    public SpaceServiceImpl(SpaceRepository spaceRepository) {
        this.spaceRepository = spaceRepository;
    }

    @Override
    public List<Space> findAll() {
        return spaceRepository.findAll();
    }

    @Override
    public Optional<Space> findById(String id) {
        return spaceRepository.findById(id);
    }

    @Override
    public PageResult<Space> findPage(PageReq pageRequest) {
        PageReq processedReq = processPageRequest(pageRequest);
        return spaceRepository.findPage(processedReq);
    }

    @Override
    @Transactional
    public Space createSpace(Space space, String userId) {
        space.setId(IdUtil.createSpaceId());
        space.setCreatedTime(LocalDateTime.now());
        space.setCreatedBy(userId);
        return spaceRepository.save(space);
    }

    @Override
    @Transactional
    public Space updateSpace(Space space, String userId) {
        space.setLastModifiedTime(LocalDateTime.now());
        space.setLastModifiedBy(userId);
        return spaceRepository.update(space);
    }

    @Override
    @Transactional
    public void deleteSpace(String id, String userId) {
        spaceRepository.softDelete(id, userId);
    }

	@Override
	protected Class<Space> getEntityClass() {
		return Space.class;
	}
}